package com.css.dzj.dzjap.modules.info.controller;

import com.alibaba.fastjson.JSON;
import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.audit.service.SysAuditService;
import com.css.dzj.dzjap.common.util.BeanUtil;
import com.css.dzj.dzjap.common.util.Constant;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.info.entity.ApExpertEntity;
import com.css.dzj.dzjap.modules.info.service.ExpertService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 专家信息
 *
 * @author liangxingguo
 */
@RestController
@RequestMapping("/info")
public class ExpertController {
    @Autowired
    ExpertService expertService;
    @Autowired
    private SysAuditService sysAuditService;
    @Autowired
    HttpServletRequest request;

    @GetMapping("/listexpert")
    public R getPageExpert(@RequestParam Integer pageNo, @RequestParam Integer pageSize,
                           @RequestParam(name = "expertName", required = false) String expertName,
                           @RequestParam(name = "expertIdcard", required = false) String expertIdcard,
                           @RequestParam(name = "endTime", required = false) String endTime,
                           @RequestParam(name = "startTime", required = false) String startTime,
                           @RequestParam(name = "expertLevl", required = false) String expertLevl,
                           @RequestParam(name = "isBlackList", required = false) String isBlackList,
                           @RequestParam(name = "expertTel", required = false) String expertTel) {
        String  s = "";
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults page = expertService.getPage(pageRequest, expertName, expertIdcard, endTime, startTime, expertLevl, isBlackList, expertTel);
            s = JSON.toJSONString(page);
            sysAuditService.saveSuccessAudit("查询成功", Constant.INFO, Constant.PT, Constant.PAGE, s, request);
            return R.ok().put("page", page);
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("查询失败", Constant.INFO, Constant.PT, Constant.PAGE, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /***
     * 逻辑删除
     * @param id
     * @return
     */
    @PostMapping("/delConstr/{id}")
    public R delExpert(@PathVariable String id) {
        String s = "";
        try {
            boolean flag = expertService.delExpert(id);
            if (flag) {
                s = JSON.toJSONString(id);
                sysAuditService.saveSuccessAudit("删除成功", Constant.INFO, Constant.PT, Constant.DEL, s, request);
                return R.ok("删除成功");
            }
            return R.error("删除失败");
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("删除失败", Constant.INFO, Constant.YZ, Constant.DEL, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /***
     * 更新专家信息
     * @param apExpertEntity
     * @return
     */
    @PostMapping("/update")
    public R updateConstr(@RequestBody ApExpertEntity apExpertEntity) {
        String s = "";
        try {
            ApExpertEntity apExpertEntity1 = expertService.findById(apExpertEntity.getExpertId());
            BeanUtil.copyBeanNotEmptyToBean(apExpertEntity, apExpertEntity1);
            apExpertEntity.setUpdateTime(new Date());
            expertService.update(apExpertEntity1);
            s = JSON.toJSONString(apExpertEntity1);
            sysAuditService.saveSuccessAudit("更新成功", Constant.INFO, Constant.PT, Constant.UPDATE, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("修改失败", Constant.INFO, Constant.YZ, Constant.UPDATE, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /***
     * 根据ID查询专家
     * @param id
     * @return
     */
    @PostMapping("/findbyid/{id}")
    public R findById(@PathVariable String id) {
        String s = "";
        try{
        ApExpertEntity apExpertEntity = expertService.findById(id);
        s = JSON.toJSONString(apExpertEntity);
        sysAuditService.saveSuccessAudit("查询成功", Constant.INFO, Constant.PT, Constant.SELECT, s, request);
        return R.ok().put("apExpertEntity", apExpertEntity);
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("修改投诉举报失败", Constant.INFO, Constant.PT, Constant.SELECT, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /***
     * 添加专家
     * @param apExpertEntity
     * @return
     */
    @PostMapping("/save")
    public R saveExpert(@RequestBody ApExpertEntity apExpertEntity) {
        String s  = "";
        try{
        apExpertEntity.setExpertCreatTime(new Date());
        apExpertEntity.setExpertDelFlag("0");
        if (StringUtils.isEmpty(apExpertEntity.getExpertName()) || StringUtils.isEmpty(apExpertEntity.getExpertIdcard())
                || StringUtils.isEmpty(apExpertEntity.getExpertLevl()) || StringUtils.isEmpty(apExpertEntity.getExpertSex())) {
            return R.error("不能为空");
        }
        if (!StringUtils.isEmpty(apExpertEntity.getExpertIdcard())) {
            ApExpertEntity apExpert = expertService.findByIdCard(apExpertEntity.getExpertIdcard());
            if (!StringUtils.isEmpty(apExpert)) {
                return R.error("该用户的证件号码已存在");
            }
        }
        expertService.save(apExpertEntity);
        s = JSON.toJSONString(apExpertEntity);
        sysAuditService.saveSuccessAudit("添加成功", Constant.INFO, Constant.PT, Constant.ADD, s, request);
        return R.ok("添加成功");
    }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("添加失败", Constant.INFO, Constant.PT, Constant.ADD, s, e, request);
            return R.error(e.getMessage());
        }
    }


}
